﻿
using Face8280WebSocketServer.DB.Log;
using Face8280WebSocketServer.Interface;
using Face8280WebSocketServer.Utility;
using Face8280WebSocketServer.Utility.FilterAttribute;
using Face8280WebSocketServer.Utility.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace Face8280WebSocketServer.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ConnectIOLogController : ControllerBase
    {

        private readonly ILogger<ConnectIOLogController> _logger;
        private IMyMemoryCache _Cache;
        private IConnectIOLogService _LogDB;

        public ConnectIOLogController(ILogger<ConnectIOLogController> logger,
            IConnectIOLogService db, IMyMemoryCache mycache)
        {
            _logger = logger;
            _LogDB = db;
            _Cache = mycache;
        }


        /// <summary>
        /// 获取日志表
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetLogs")]
        [Authorize]
        [TypeFilter(typeof(VerifyActionFilterAttribute))]
        public IActionResult GetReport([FromForm] IOLogQueryParameter par)
        {
            List<Expression<Func<ConnectIOLog, bool>>> oWheres = new List<Expression<Func<ConnectIOLog, bool>>>();
            oWheres.Add(x => x.LogTime >= par.LogTimeBegin && x.LogTime <= par.LogTimeEnd);

            if (!string.IsNullOrWhiteSpace(par.DeviceSN)) oWheres.Add(x => x.DeviceSN.Contains(par.DeviceSN));
            if (!string.IsNullOrWhiteSpace(par.HttpType)) oWheres.Add(x => x.HttpType.Contains(par.HttpType));
            if (!string.IsNullOrWhiteSpace(par.IPAddr)) oWheres.Add(x => x.IPAddr.Contains(par.IPAddr));
            if (!string.IsNullOrWhiteSpace(par.URL)) oWheres.Add(x => x.URL.Contains(par.URL));
            if (!string.IsNullOrWhiteSpace(par.Method)) oWheres.Add(x => x.Method.Contains(par.Method));
            if (!string.IsNullOrWhiteSpace(par.RequestID)) oWheres.Add(x => x.RequestID.Contains(par.RequestID));
            if (!string.IsNullOrWhiteSpace(par.NotShowURL)) oWheres.Add(x => !x.URL.Contains(par.NotShowURL));

            //_logger.LogInformation("获取通讯日志列表");
            var reports = _LogDB.QueryPage(
                oWheres, par.pageSize, par.pageIndex,
                x => x.LogTime,
                par.isAsc);

            return new JsonResult(new JsonResultModel(reports));
        }


        /// <summary>
        /// 删除日志
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("DeleteLogs")]
        [Authorize]
        public IActionResult DeleteLogs()
        {
            //_logger.LogInformation("清空通讯日志列表");
            var reports = _LogDB.ClearLog();

            return new JsonResult(new JsonResultModel());
        }
    }
}
